home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group95c.txt / 000084_icon-group-sender _Sat Nov 18 15:55:45 1995.msg < prev    next >
Internet Message Format  |  1996-01-03  |  6KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 22 Nov 1995 10:44:02 MST
  2. To: icon-group@cs.arizona.edu
  3. Date: 18 Nov 1995 15:55:45 +0100
  4. From: janpeter@mpi.nl (Jan-Peter de Ruiter)
  5. Message-Id: <qjraz6dlam.fsf_-_@mpih16.mpi.nl>
  6. Organization: Max-Panck Institut f|r Psycholinguistik, Nijmegen, the Netherlands
  7. Sender: icon-group-request@cs.arizona.edu
  8. References: <485l4c$erl@newshost.nmt.edu>
  9. Subject: Mathematical sets in Icon (Re: Comparing sets for equality)
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. In article <485l4c$erl@newshost.nmt.edu> john@nmt.edu (John Shipman) writes:
  13.  
  14.    I need an Icon function to test two sets to see if they're identical.
  15.  
  16.    Is there an operator that does this, that I have somehow missed?
  17.  
  18. No, this is a problem. A few years ago we discussed this problem here
  19. too, and Icon's definition of identical is not the mathematical
  20. definition, because implementing it in the mathematically correct way
  21. will decrease Icon's performance, if I remember correctly.
  22.  
  23. Your solution will only work partially. If you insert the same string
  24. twice, Icon will think there's two elements in your set, which is not
  25. the way it should work, if you think of sets as mathematical sets.
  26.  
  27. Here's a complete implementation that I wrote to solve this problem.
  28. It can also be used to check if two Icon values are identical in the
  29. sense that they have the same values (not that they refer to the same
  30. objects). If there's anyone from the Icon project who likes to put
  31. this extension in the IPL, please be my guest.
  32.  
  33. Greetings,
  34.  
  35. Jan
  36.  
  37. #############################################################################
  38. # MSET LIBRARY                                                              #
  39. #                                                                           #
  40. # by Jan P. de Ruiter, 1992                                                 #
  41. # Public domain software.                                                   #
  42. #                                                                           #
  43. # Implements the "mset" type.                                               #
  44. #                                                                           #
  45. # The idea of the mset type is that no two identical data-structures can be #
  46. # present in a set, where identity is defined as "containing the same       #
  47. # elements".                                                                #
  48. #                                                                           #
  49. # This seemingly radical idea is actually quite common in mathematics. :-)  #
  50. #                                                                           #
  51. # Definitions implicit in the procedure same_value(..,..):                  #
  52. #                                                                           #
  53. # TYPE              IDENTITY TEST                                           #
  54. #                                                                           #
  55. # all types            ===          and if this test fails...               #
  56. #                                                                           #
  57. # integer               =                                                   #
  58. # real                  =                                                   #
  59. # cset, string          ==                                                  #
  60. # record            all fields have same value                              #
  61. # list              all elements are the same, and the ordering is the same #
  62. # table             same keys, and every key has the same associated value  #
  63. # set               contain the same elements                               #
  64. #                                                                           #
  65. #############################################################################
  66.  
  67. #
  68. # This is the core routine.
  69. # It succeeds if two things have the same value(s).
  70. #
  71. procedure same_value(d1,d2)
  72.     if d1 === d2 then return              # same object
  73.     else
  74.     if type(d1) ~== type(d2) then fail  # not the same type
  75.     else
  76.     if *d1 ~= *d2 then fail             # not the same size
  77.     else
  78.     case type(d1)  of {                 # the same type and size
  79.        ("set"  | "table"  ) : return same_elements(sort(d1,1),sort(d2,1))
  80.        ("list")             : return same_elements(d1,d2)
  81.        ("real" | "integer") : return(d1 = d2)
  82.        ("cset" | "string" ) : return(d1 == d2)
  83.        default              : return same_elements(d1,d2) # user defined type
  84.     }
  85. end
  86.  
  87. #
  88. # used in same_value:
  89. #
  90.  
  91. procedure same_elements(l1,l2)
  92.     if l1 === l2 then return   # same objects
  93.     else
  94.     if *l1 ~= *l2 then fail    # not the same size
  95.     else {
  96.         if *l1 = 0 then return # both lists empty
  97.         else {
  98.             every(i := 1 to *l1) do 
  99.                 if not same_value(l1[i],l2[i]) then fail  # recursion
  100.             return
  101.         }
  102.     }
  103. end
  104.     
  105. #
  106. # The new insert operation. Insert2 always succeeds
  107. #
  108. procedure insert2(S,el)
  109.     if same_value(el,!S) then return
  110.     return insert(S,el)
  111. end     
  112.  
  113. #
  114. # The new member operation, that also detects equal-valued elements
  115. #
  116. procedure member2(S,el)
  117.     if same_value(!S,el) then return
  118.     fail
  119. end
  120.  
  121. #
  122. # The new delete operation, that detects equal-valued elements.
  123. # Always succeeds
  124. #
  125. procedure delete2(S,el)
  126.     every(t := !S) do if same_value(t,el) then return delete(S,t)
  127.     return
  128. end
  129.  
  130. #
  131. # conversion of standard icon set into new mset.
  132. #
  133. procedure reduce(iset)
  134.     temp := set()
  135.     every(insert2(temp,!iset))
  136.     return temp
  137. end
  138.  
  139.  
  140.